<template>
	<view class="container">

		<view class="user-section">
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" :src="userInfo.photoUrl || '/static/image/missing-face.png'"></image>
				</view>
				<view class="name">
					<view v-if="hasLogin" @click="navTo('/pages/set/set-user-info-name')">
						{{userInfo.name||'设置昵称'}}
					</view>
					<view v-else @click="navTo('/pages/public/login')">
						立即登录
					</view>
					<view class="level" v-if="hasLogin" @click="navTo('/pages/user/user-upgrade-list')">
						{{userInfo.userLevelName}}<text v-if="userInfo.userLevelExpireDate">(有效期至{{userInfo.userLevelExpireDate}})</text>
					</view>
				</view>
				<view class="action" v-if="hasLogin" @click="navTo('/pages/user/user-upgrade')">
					<span v-if="userInfo.userLevelDTO.default">升级会员</span>
					<span v-else>续费</span>
				</view>
			</view>
		</view>
		<view class="cover-container" :style="[{
				transform: coverTransform,
				transition: coverTransition
			}]"
		 @touchstart="coverTouchstart" @touchmove="coverTouchmove" @touchend="coverTouchend">

			<view class="tj-sction">
				<view class="tj-item">
					<text class="num" @click="navTo('/pages/user/balance')">{{userInfo.availableBalance||0}}</text>
					<text>余额</text>
					<!-- <text class="b-btn" @click="navTo('/pages/withdraw/list')">提现</text> -->
				</view>
				<view class="tj-item">
					<text class="num" @click="navTo('/pages/point/point-list')">{{userInfo.availablePoint||0}}</text>
					<text>积分</text>
				</view>
				<view class="tj-item">
					<text class="num" @click="navTo('/pages/user/coupon')">{{userInfo.couponCount||0}}</text>
					<text>优惠券</text>	
				</view>
			</view>

			<!-- 广告区域 -->
			<view class="ad-1" v-if="ads.length>0">
				<image @click="navAD(ads[0])" :src="ads[0].url" mode="scaleToFit"></image>
			</view>
			
			<view class="action-container">
				<view class="button-base-outline" @click="navTo('/pages/point/sign')">
					<text class="iconfont icon-sign-board"></text>签到
				</view>
				<view class="button-base-outline" @click="navTo('/pages/coupon/coupon-list')">
					<text class="iconfont icon-coupon"></text>领券
				</view>
				<view class="button-base-outline" @click="navTo('/pages/point/product-list')">
					<text class="iconfont icon-point"></text>积分兑换
				</view>
			</view>
			
			<view class="order-section">
				<view class="order-item" @click="navTo('/pages/user/resource')" hover-class="common-hover" :hover-stay-time="50">
					<i class="iconfont icon-product-list-fill"></i>
					<span>已购资源</span>
				</view>
				<view class="order-item" @click="navTo('/pages/point/point-order-list')" hover-class="common-hover" :hover-stay-time="50">
					<i class="iconfont icon-point"></i>
					<span>兑换记录</span>
				</view>
				<view class="order-item" @click="navTo('/pages/address/address')" hover-class="common-hover" :hover-stay-time="50">
					<i class="iconfont icon-location-fill"></i>
					<span>地址管理</span>
				</view>
				<view class="order-item" @click="navTo('/pages/resource/favorite')" hover-class="common-hover" :hover-stay-time="50">
					<i class="iconfont icon-favorites-fill"></i>
					<span>我的收藏</span>
				</view>
			</view>
			<!-- 浏览历史 -->
			<view class="history-section">
				<list-cell  v-if="hasLogin" @eventClick="togglePopup('bottom', 'share')" icon="icon-share-fill" iconColor="#43A49B" title="分享" tips="邀请好友赢取佣金"></list-cell>
				<list-cell icon="icon-notice-fill" iconColor="#43A49B" title="消息中心" border="" @eventClick="navTo('/pages/notice/list')"></list-cell>
				<list-cell icon="icon-customer-service-fill" iconColor="#43A49B" title="客服中心" border="" @eventClick="navTo('/pages/help/help')"></list-cell>
				<list-cell icon="icon-settings-fill" iconColor="#43A49B" title="设置" border="" @eventClick="navTo('/pages/set/set')"></list-cell>
			</view>
		</view>
		<!-- 底部分享弹窗 -->
		<uni-popup ref="showshare" :type="type">
			<!-- #ifdef H5 -->
			<shareByH5 :shareType='1' :userId="userInfo.userUuid" :shareHref="shareHref" @close="closeShare()"></shareByH5>
			<!-- #endif -->

			<!-- #ifdef MP-WEIXIN -->
			<shareByWx :shareType='1' :userId="userInfo.userUuid" :shareImg="userInfo.photoUrl"
			 shareTitle="注册会员, 立享海量资源!" :shareContent="'我是'+userInfo.name" :shareHref="'/pages/public/login?id='+userInfo.userUuid" @close="closeShare()"></shareByWx>
			<!-- #endif -->

			<!-- #ifdef MP-ALIPAY -->
			<shareByAli :shareType='1' :userId="userInfo.userUuid" :shareHref="shareHref" @close="closeShare()"></shareByAli>
			<!-- #endif -->

			<!-- #ifdef APP-PLUS -->
			<shareByApp :shareType='1' :userId="userInfo.userUuid" :shareHref="shareHref" @close="closeShare()"></shareByApp>
			<!-- #endif -->
		</uni-popup>

	</view>
</template>
<script>
	import listCell from '@/components/list-cell';
	import uniBadge from "@/components/uni-badge/uni-badge.vue";
	import uniPopup from "@/components/uni-popup/uni-popup.vue"
	// #ifdef H5
	import shareByH5 from '@/components/share/shareByh5.vue'
	// #endif
	// #ifdef MP-WEIXIN
	import shareByWx from '@/components/share/shareByWx.vue'
	// #endif
	// #ifdef MP-ALIPAY
	import shareByAli from '@/components/share/shareByAli.vue'
	// #endif
	// #ifdef APP-PLUS
	import shareByApp from '@/components/share/shareByApp.vue'
	// #endif
	import {
		mapState,
		mapMutations
	} from 'vuex';
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {
			listCell,
			uniBadge,
			uniPopup,
			// #ifdef H5
			shareByH5,
			// #endif
			
			// #ifdef MP-WEIXIN
			shareByWx,
			// #endif
			
			// #ifdef MP-ALIPAY
			shareByAli,
			// #endif
			
			// #ifdef APP-PLUS
			shareByApp,
			// #endif
		},
		data() {
			return {
				type: '',
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				ads: [], //广告
				moving: false
			}
		},
		onLoad() {
			this.inquiryAd();
		},
		onShareAppMessage(res) {
		    return {
		      title: '我是'+this.userInfo.name+',注册会员,立享海量资源!',
			  imageUrl:this.userInfo.photoUrl,
		      path: '/pages/public/login?id=' + this.userInfo.userUuid
		    }
		 },
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/list'
				})
			}
		},
		// #endif
		computed: {
			...mapState(['hasLogin', 'userInfo', 'footPrint', 'applicationConfig']),
			shareHref() {
				let pages = getCurrentPages()
				let page = pages[pages.length - 1]
				// #ifdef H5 || MP-WEIXIN || APP-PLUS || APP-PLUS-NVUE
				return this.$api.request.apiBaseUrl.replace("/cvc/rest/","") + '/#/pages/public/login?id=' + this.userInfo.userUuid;
				// #endif
			
				// #ifdef MP-ALIPAY
				return this.$api.request.apiBaseUrl.replace("/cvc/rest/","") + '/#/pages/public/login?id=' + this.userInfo.userUuid;
				// #endif
			}
		},
		methods: {
			...mapMutations(['login']),
			//首页广告
			inquiryAd() {
				this.$api.request.advert({
					location: '会员中心页'
				}, res => {
					if (res.body.status.statusCode === '0') {
						this.ads = res.body.data.ads;
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			//广告链接
			navAD(item) {
				this.$api.util.navAD(item);
			},
			toggleSignPoint: function () {
			  uni.navigateTo({
				url:'/pages/point/sign'
			  })
			},
			togglePopup(type, open) {
				this.type = type
				this.$nextTick(() => {
					this.$refs['show' + open].open()
				})
			},
			closeShare() {
				this.$refs.showshare.close();
			},
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url
				})
			},

			customerService() {
				uni.navigateTo({
					url:'/pages/help/help'
				})
			},
			
			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e) {
				if (pageAtTop === false) {
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e) {
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if (moveDistance < 0) {
					this.moving = false;
					return;
				}
				this.moving = true;
				if (moveDistance >= 80 && moveDistance < 100) {
					moveDistance = 80;
				}

				if (moveDistance > 0 && moveDistance <= 80) {
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend() {
				if (this.moving === false) {
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			}
		},
		onShow() {
			//同步用户信息
			if (this.hasLogin) {
				this.$api.request.userInfo({
					userUuid: this.userInfo.userUuid
				}, res => {
					if (res.body.status.statusCode === '0') {
						this.login(res.body.data);
					}
				});
			}
		}
	}
</script>
<style lang='scss'>
	page{
		background: $bg-color-light;
	}
	.container{
		margin: 0 $page-row-spacing;
	}
	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: $bg-color-base;
		border-radius: 10upx;
	}

	.user-section {
		height: 400upx;
		position: relative;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .7;
		}
	}

	.user-info-box {
		padding-top: 200upx;
		display: flex;
		align-items: center;
		position: relative;
		z-index: 1;

		.portrait-box {
			margin-right: 20upx;
		
			image {
				width: 120upx;
				height: 120upx;
				border-radius: 50%;
			}
		}
		.name {
			color: $font-color-dark;
			font-weight: bold;
			font-size: $font-lg;
			.level {
				color: $font-color-base;
				font-weight: normal;
				font-size: $font-sm;
				margin: 10upx 0;
			}
		}
		.level {
			margin-left: auto;
			color: $font-color-dark;
		}

		.username {
			font-size: $font-sm;
			color: $font-color-base;
			margin-left: 20upx;
		}
		
		.action{
			background: transparent;
			border: 1px solid $border-color-focus;
			color: $bg-color-dark;
			font-size: $font-sm;
			margin-left: auto;
			padding: 8upx 16upx;
			width: 140upx;
			text-align:center;
		}
	}

	.cover-container {
		position: relative;
		padding-bottom: 20upx;
	}
	
	// 广告栏区域
	.ad-1 {
		// width: 100%;
		height: 210upx;
		margin: 10upx 0;
		background: #fff;
	
		image {
			width: 100%;
			height: 100%;
		}
	}

	.tj-sction {
		@extend %section;
		align-items: baseline;
		background: $bg-color-dark;
		padding: 40upx 0 20upx 0;
		.tj-item {
			@extend %flex-center;
			flex-direction: column;
			height: 140upx;
			font-size: $font-sm;
			color: #FFFFFF;
			.b-btn {
				margin: 10px 0;
				width: 132upx;
				height: 40upx;
				text-align: center;
				line-height: 40upx;
				font-size: 22upx;
				color: #26A69A;
				border-radius: 10px;
				background: #D0EBEA;
				z-index: 1;
			}
		}

		.num {
			font-size: $font-lg;
			font-weight: bold;
			color: #ffffff;
			margin-bottom: 8upx;
		}
	}
	
	.action-container{
		display: flex;
		.iconfont{
			font-size: $font-lg;
			margin: 0 10upx;
		}
	}
	.button-base-outline{
		margin: 20upx 20upx 20upx 0;
		border-radius: 10upx;
		border: 5upx;
		width: 30%;
		background: $bg-color-base;
		color: $font-color-selected;
	}

	.order-section {
		@extend %section;
		padding: 28upx 0;

		.order-item {
			position: relative;
			@extend %flex-center;
			width: 120upx;
			height: 120upx;
			border-radius: 10upx;
			.iconfont{
				color: $base-color;
				font-size: 80upx;
			}
			span{
				color: $font-color-base;
				font-size: $font-sm;
				margin-top: 5upx;
			}
		}
		.uni-badge{
			position: absolute;
			top: -2px;
			left: 30px;
			background: $bg-color-dark;
			color: #fff;
		}
	}

	.history-section {
		margin: 30upx 0 0;
		border-radius: 10upx;
		background: #fff;
		.sec-header {
			display: flex;
			align-items: center;
			font-size: $font-base;
			color: $font-color-dark;
			line-height: 40upx;
			margin-left: 30upx;

			.iconfont {
				font-size: 44upx;
				color: #43A49B;
				margin-right: 16upx;
				line-height: 40upx;
			}
		}

		.h-list {
			white-space: nowrap;
			padding: 30upx 30upx 0;

			image {
				display: inline-block;
				width: 160upx;
				height: 160upx;
				margin-right: 20upx;
				border-radius: 10upx;
			}
		}
	}
	.nearby{
		opacity: .5;
		background-color: #000;
		position: absolute;
		right: 0px;
		top: 150upx;
		width: 200upx;
		height: 60upx;
		line-height: 60upx;
		z-index: 999;
		color: #fff;
		font-size: $font-base;
		border-radius: 100upx 0px 0px 100upx;
		text-align: center;
	}
</style>
